$(function() {
  $(':button#btnpack').bind('click', function() {
    $.getJSON('/_packflow', {
      boxid: $('input[name="boxinput"]').val(),
      baseqty: $('input[name="baseqty"]').val(),
      ssn: $('input[name="ssn"]').val(),
      line: $("#line").html(),
      failcode: $('select[name="failcode"]').val(),
      date: new Date().getTime()+Math.random()
    }, function(data) {
      if(data.result == 'RELOGIN')
      {
        location.href = "/login";
      }
      else
      {
        $("#result").text(data.result);
        if($("#result").text() == "" || $("#result").text() == "FULL")
        {
          var pqty = data.packedqty;
          var d = new Date().format("yyyy-MM-dd hh:mm:ss");
          var s_ssn = ssn.value;
          var s_box = $("#boxinput").val();

          if (pqty != "")
          {
            if(pqty != "0")
            {
              document.getElementById("packqty").innerText = String(pqty);
            }
            else
            {
              document.getElementById("baseqty").innerText = "";
              document.getElementById("packqty").innerText = "0";
              document.getElementById("boxinput").innerText = "";
            }            
            $("#flowdetail").val(d + "   " + s_ssn + " packed in Box: " + s_box + "\r" + $("#flowdetail").text());
          }
        }
        else
        {
          alert(data.result);
        }
        ssn.value = "";
      }
    });
    return false;
  });
})

$(document).ready( function() {
  $("#frccls").click( function() {
    if($("#boxinput").val() != "")
    {
      var fcs = confirm("Force close the BOX: " + $("#boxinput").val() + " , Are you sure?");
      if(fcs)
      {            
        $.getJSON('/_forceclose', {
        boxid: $('input[name="boxinput"]').val(),
        date: new Date().getTime()+Math.random()
      }, function(data) {
        if(data.result == 'RELOGIN')
        {
          location.href = "/login";
        }
        else
        {
          $("#result").text(data.result);
          if($("#result").text() == "")
          {
            var d = new Date().format("yyyy-MM-dd hh:mm:ss");
            var s_box = $("#boxinput").val();
            document.getElementById("baseqty").innerText = "0";
            document.getElementById("packqty").innerText = "0";
            document.getElementById("boxinput").innerText = "";
            $("#flowdetail").val(d + "   " + s_box + " has been FORCE CLOSED.\r" + $("#flowdetail").text());
          }
        }
      });            
      }
    }
  });
})

function txtKeyDown()
{
  if(event.keyCode == 13)
  {
    btnpack.click();
    ssn.focus();
  }
}
